﻿using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Product.Domain.Entity;
using Product.EntityFrameworkCore.PO;

namespace Product.EntityFrameworkCore.MapConfiguration
{
    public class CategoryMapConfiguration : IEntityTypeConfiguration<CategoryPo>
    {
        public void Configure(EntityTypeBuilder<CategoryPo> builder)
        {
            builder.ToTable("t_category");
            builder.HasKey("Id");
            builder.Property(e => e.Id).HasMaxLength(64).IsRequired();
            builder.Property(e => e.CategoryLevel).IsRequired();
            builder.Property(e => e.CategoryName).HasMaxLength(100).IsRequired();
            builder.Property(e => e.ParentId).HasMaxLength(64).IsRequired(false);
            builder.Property(e => e.Version).IsRequired();

            builder.HasMany(x => x.ChildCategoryList)
                .WithOne(op => op.ParentCategory)
                .OnDelete(DeleteBehavior.Restrict)
                .IsRequired(false)
                .HasForeignKey(e => e.ParentId);

            builder.HasOne(x => x.ParentCategory)
                .WithMany(op => op.ChildCategoryList)
                .OnDelete(DeleteBehavior.Restrict)
                .IsRequired(false)
                .HasForeignKey(e => e.ParentId);
        }
    }
}
